import {
    deletePlayerData,
    getPlayerData,
    isPlayerExist,
    storagePlayerData
} from '../function/function.js'

Bot.on?.('notice.group', async (e) => {
    switch (e.sub_type) {
        case 'decrease': {
            if (e.operator_id === e.user_id) {
                if (e.user_id === e.self_id) return false

                const ID = [e.user_id, e.group_id]
                if (!isPlayerExist(ID[1], ID[0])) return false

                const userData = await getPlayerData(ID[1], ID[0])
                const { master, slave } = userData
                if (master !== '') {
                    const masterData = await getPlayerData(ID[1], master)
                    masterData['slave'] = masterData['slave'].filter(a => a !== ID[0]);
                    await storagePlayerData(ID[1], master, masterData)
                }
                if (slave.length !== 0) {
                    for (let i = 0; i < slave.length; i++) {
                        const slaveId = slave[i]
                        const slaveData = await getPlayerData(ID[1], slaveId)
                        slaveData['master'] = ''
                        await storagePlayerData(ID[1], slaveId, slaveData)
                    }
                }
                deletePlayerData(ID[1], ID[0])
                return false
            }
        }
    }
})